/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sim.helpers;

import sim.coalitional.Head;
import sim.coalitional.Node;

/**
 *
 * @author artem
 */
public class Helper {

    public double SD;
    public double QOS;
    public int N;
    public int L;
    public int H;
    public int R;
    public int Energy = 10000;

    public Helper() {
    }

    public double distance(int xa, int ya, int xb, int yb) {
        return Math.sqrt(Math.pow(xa - xb, 2) + Math.pow(ya - yb, 2));
    }

    public double distance(Node a, Node b) {
        int xa = a.getCoor().getX();
        int ya = a.getCoor().getY();
        int xb = b.getCoor().getX();
        int yb = b.getCoor().getY();
        return Math.sqrt(Math.pow(xa - xb, 2) + Math.pow(ya - yb, 2));
    }

    public double distance(Node a, Head h) {
        int xa = a.getCoor().getX();
        int ya = a.getCoor().getY();
        int xh = h.getCoor().getX();
        int yh = h.getCoor().getY();
        return Math.sqrt(Math.pow(xa - xh, 2) + Math.pow(ya - yh, 2));
    }

    public double pdf(double distance, double sd) {
        return Math.exp((-1.0) * Math.pow(distance, 2) / Math.pow(sd, 2));
//        return 1 / distance;
    }

    public double getDistanceForQos(double QoS, double sd, double maxDistance) {
        double ret = 0;
        double precision = 0.1;
        int loopCount = (int) (maxDistance / precision);
        double currentValue = 0;
        boolean found = false;
        double normalizator = pdf(0, sd);
        for (int i = 0; i < loopCount; i++) {
            double currentDistance = i * precision;
            currentValue = pdf(currentDistance, sd) / normalizator;
            if (currentValue < QoS) {
                ret = (i - 1) * precision;
                found = true;
                break;
            }
        }
        if (!found) {
            ret = loopCount * precision;
        }
        return ret;
    }

    public double getMaximumIntraCoalitionDistance() {
        return getDistanceForQos(QOS / 100, SD, L);
    }
    
    public Helper clone(){
        Helper ret = new Helper();
        ret.Energy = Energy;
        ret.H = H;
        ret.L = L;
        ret.N = N;
        ret.QOS = QOS;
        ret.R = R;
        ret.SD = SD;
        return ret;
    }
}
